package com.linln.modules.userule.rule;

import com.linln.modules.area.service.AreaService;
import com.linln.modules.shopsService.domain.Shops;
import com.linln.modules.shopsService.service.ShopsService;
import com.linln.modules.ticket.domain.Ticket;
import com.linln.modules.ticket.rule.Rule;
import com.linln.modules.ticket.rule.RuleResult;
import com.linln.modules.userule.domain.UseRule;
import com.linln.modules.userule.service.UseRuleService;
import org.apache.logging.log4j.util.Strings;
import org.elasticsearch.index.fielddata.ScriptDocValues;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author : pengliang
 * @version V1.0
 * @Project: timo
 * @Package com.linln.common.rules
 * @Descriptio /**
 * @Project: timo
 * @Package com.linln.common.rules
 * @Description: TODO
 * @date Date : 2020年11月25日 16:19
 */
@Component
@Order(Integer.MAX_VALUE)
public class DefaultRule implements Rule {
    @Autowired
    UseRuleService useRuleService;
    @Autowired
    private AreaService areaService;
    @Autowired
    private ShopsService shopsService;

    @Override
    public RuleResult handlerCode(String userId, Ticket ticket, String shopId) {
        if (Strings.isBlank(ticket.getUseRuleId())) {
            return RuleResult.ok();
        }
        UseRule useRule = useRuleService.getById(ticket.getUseRuleId());
        Map<String, String> jsonUseRuleProps = useRule.getJsonUseRuleProps();

        if (jsonUseRuleProps != null) {
//            if(!shopId.equals(shopId)){
//                return RuleResult.error("仅限指定店铺使用");
//            }
            if (!Strings.isBlank(jsonUseRuleProps.get("shopId"))) {
                if (!shopId.equals(jsonUseRuleProps.get("shopId"))) {
                    return RuleResult.error("该优惠券仅指定店铺可用");
                }
            }

            Shops shops = shopsService.getById(shopId);
            String areaId = jsonUseRuleProps.get("rearId");
            if (!Strings.isBlank(areaId)) {
                List<Integer> sonIds = areaService.getSonIds(Integer.valueOf(areaId));
                if (!sonIds.contains(shops.getAreaId())) {
                    return RuleResult.error("该优惠券仅指定地区可用");
                }
            }

            String shopType = jsonUseRuleProps.get("shopType");
            if (!Strings.isBlank(shopType)) {
                String[] shopTypes = shopType.split(",");
                List<String> currentShopTypes = Arrays.asList(shops.getShopType().split(","));
                boolean flag = true;
                for (String item : shopTypes) {
                    if (currentShopTypes.contains(item)) {
                        flag = false;
                        break;
                    }
                }
                if (flag) {
                    return RuleResult.error("该优惠券仅指定店铺类型可用");
                }
            }

        }

        return RuleResult.ok();
    }
}
